package com.example.yls.demoa;

import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //按钮1
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //simplyDialog1();
                simplyDialog1_1();
            }
        });
        //按钮2
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                simplyDialog2();
            }
        });
        //按钮3
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                simplyDialog3();
            }
        });

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                simplyDialog4();
            }
        });
    }

    private void simplyDialog4() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("请输入你的帐号和密码")
                .setIcon(R.mipmap.ic_launcher_round);
        final View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog,null);
        builder.setView(view);

        final EditText username = view.findViewById(R.id.username);
        final EditText password = view.findViewById(R.id.password);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                String a = username.getText().toString().intern();
                String b = password.getText().toString().intern();

                Toast.makeText(MainActivity.this,"用户名："+a+" ,密码："+b,Toast.LENGTH_LONG).show();

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.show();
    }

    private void simplyDialog3() {
        final String item[] = {"篮球" ,"足球","羽毛球","兵兵球"};
        final boolean selected[]={true,false,true,false};
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("请选择你喜欢的运动")
                .setIcon(R.mipmap.ic_launcher_round);
        builder.setMultiChoiceItems(item, selected, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                Toast.makeText(MainActivity.this,item[i]+b,Toast.LENGTH_LONG).show();
            }
        });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this,"确认" + i, Toast.LENGTH_LONG).show();
                for(int j =0;j>selected.length;j++){
                    Log.e("hongliang",""+selected[j]);
                }
            }
        });
        builder.create().show();
    }


    private void simplyDialog2() {
        final String item[] = {"男" ,"女"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("请选择性别")
                .setIcon(R.mipmap.ic_launcher_round);
        builder.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this,item[i],Toast.LENGTH_LONG).show();
            }
        });
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this,"确认" + i, Toast.LENGTH_LONG).show();
            }
        });
        builder.create().show();

    }

    private void simplyDialog1_1() {
        DialogInterface.OnClickListener dialogOnClicListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                switch (i){
                    case Dialog.BUTTON_POSITIVE:
                        Toast.makeText(MainActivity.this,"确认" + i, Toast.LENGTH_LONG).show();
                        break;
                    case Dialog.BUTTON_NEGATIVE:
                        Toast.makeText(MainActivity.this,"取消" + i, Toast.LENGTH_LONG).show();
                        break;
                    case Dialog.BUTTON_NEUTRAL:
                        Toast.makeText(MainActivity.this,"忽略" + i, Toast.LENGTH_LONG).show();
                }
            }
        };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("警告");
        builder.setMessage("电脑已中毒");
        builder.setPositiveButton("确认",dialogOnClicListener);
        builder.setNegativeButton("取消",dialogOnClicListener);
        builder.setNeutralButton("忽略",dialogOnClicListener);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.create().show();
    }

    /*private void simplyDialog1() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("警告");
        builder.setMessage("电脑已中毒");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                Toast.makeText(MainActivity.this,"确认" + i, Toast.LENGTH_LONG);
            }
        });

        builder.setNegativeButton("忽略", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                Toast.makeText(MainActivity.this,"忽略" + i, Toast.LENGTH_LONG);

            }
        });
        builder.create().show();
    }*/
}
